package users

import (
	"time"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
)

// 创建用户信息
// 此操作并不会记录日志，请手动记录相关日志信息
//
//	mod	待创建的用户信息
func Create(mod *model.User) error {
	mod.CreatedAt = time.Now().Unix()
	conn, err := config.GetConnect()
	if err != nil {
		return err
	}
	if mod.Password != "" {
		mod.Password = passwd(mod.Password)
	}
	if mod.Id > 0 {
		old := model.User{}
		err = conn.Table(mod.TableName()).Where("id", mod.Id).Order("id ASC").First(&old).Error
		if err != nil {
			return err
		}
		mod.CreatedAt = old.CreatedAt
		if mod.Password == "" {
			mod.Password = old.Password
		}
	}
	return conn.Table(mod.TableName()).Save(&mod).Error
}
